package com.neusoft.chenhanhua.d0923;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import org.junit.Test;


public class Wlbc {
   public static void main(String[] args) {
	t1();
}
   private static void t1() {
	   try {
		InetAddress ai=InetAddress.getLocalHost();//查本机ip
		//System.out.println(ai);
		//System.out.println(InetAddress.getByName("192.168.24.1").getCanonicalHostName());
		System.out.println(ai.getCanonicalHostName());
	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
   }
   
   @Test//不能是static方法
	public void t2Server() {
		try {
			System.out.println("启动服务端");
			ServerSocket server = new ServerSocket(9527);//确定使用端口号
			Socket accept = server.accept();//等socket客户端连接
//			System.out.println(accept);
			
			while (true) {
				InputStream is = accept.getInputStream();
				byte[] b = new byte[1024];
				is.read(b);
				String er=new String(b);
				System.out.println(er);
				if (er=="beybey") {
					break;
				}
					String inq=new Scanner(System.in).next();
					OutputStream os = accept.getOutputStream();
					os.write(inq.getBytes());
			}
			//System.out.println("服务端关闭");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void t2Client() {
		try {
			Socket s = new Socket(InetAddress.getByName("192.168.1.31"), 9528);
			OutputStream os = s.getOutputStream();
			os.write("这是客户端信息".getBytes());
			InputStream is = s.getInputStream();
			byte[] b = new byte[1024];
			is.read(b);
			System.out.println(new String(b));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
